/**
 *
 */
package strategies;

import com.dukascopy.api.*;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Class for dumping the data for simulator
 * @author kushal
 */
public class DummyStrategy extends Strategy {

    // period and file writer map
    private Map<String, PrintWriter> data = new HashMap<String, PrintWriter>();

    // Date format
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    // Indicates whether to log data or not
    private boolean logData;

    /**
     * Creates new instance of DummyStrategy
     * @param arguments strategy arguments
     * @param logData whether to log data or not
     * @param filePrefix prefix of the data directory that will be created
     */
    public DummyStrategy(StrategyArgs arguments, boolean logData, String filePrefix) throws IOException {
        // Update the arguments
        super(arguments);

        // Set logData flag
        this.logData = logData;

        // Create the period and the file writer map
        if (logData) {
            // Create the folder to hold data
            String folder = filePrefix + "_"
                    + format.format(arguments.getStartDate()) + " to "
                    + format.format(arguments.getEndDate()) + "_"
                    + new Date().getTime();
            new File(folder).mkdir();

            // Create the ask and bid files for all periods
            for (Period p : Period.values()) {
                // Tick doesn't have bid and ask
                if (p.name() != "TICK") {
                    data.put(p.name() + "_ASK", new PrintWriter(
                            new BufferedWriter(new FileWriter(folder + "/"
                                    + p.name() + "_ASK.csv"), 512 * 1024)));
                    data.put(p.name() + "_BID", new PrintWriter(
                            new BufferedWriter(new FileWriter(folder + "/"
                                    + p.name() + "_BID.csv"), 512 * 1024)));
                } else
                    data.put(p.name(), new PrintWriter(
                            new BufferedWriter(new FileWriter(folder + "/"
                                    + p.name() + ".csv"), 1024 * 1024)));
            }
        }
    }

    /*
      * (non-Javadoc)
      *
      * @see com.dukascopy.api.IStrategy#onAccount(com.dukascopy.api.IAccount)
      */
    @Override
    public void onAccount(IAccount account) throws JFException {
    }

    /*
      * (non-Javadoc)
      *
      * @see com.dukascopy.api.IStrategy#onBar(com.dukascopy.api.Instrument,
      * com.dukascopy.api.Period, com.dukascopy.api.IBar, com.dukascopy.api.IBar)
      */
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar,
                      IBar bidBar) throws JFException {
        // Log data only for EURUSD
        if (instrument == Instrument.EURUSD && logData) {
            data.get(period.name() + "_ASK").write(barToString(askBar));
            data.get(period.name() + "_BID").write(barToString(bidBar));
        }

        // Print the daily progress
        if (period == Period.DAILY) {
            System.out.println(String.format(
                    "%1$s,%2$1.6f,%2$1.6f,%2$1.6f,%2$1.6f", new Date(askBar
                    .getTime()), askBar.getOpen(), askBar.getHigh(),
                    askBar.getLow(), askBar.getClose()));
        }
    }

    /**
     * Convert a bar to comma separated string
     * @param bar to be converted
     * @return string representation of the bar
     */
    public String barToString(IBar bar) {
        return String.format("%1$d,%2$1.6f,%3$1.6f,%4$1.6f,%5$1.6f\r\n", bar
                .getTime(), bar.getOpen(), bar.getHigh(), bar.getLow(), bar
                .getClose());
    }

    /*
      * (non-Javadoc)
      *
      * @see com.dukascopy.api.IStrategy#onMessage(com.dukascopy.api.IMessage)
      */
    @Override
    public void onMessage(IMessage message) throws JFException {
    }

    /*
      * (non-Javadoc)
      *
      * @see com.dukascopy.api.IStrategy#onStart(com.dukascopy.api.IContext)
      */
    @Override
    public void onStart(IContext context) throws JFException {
    }

    /*
      * (non-Javadoc)
      *
      * @see com.dukascopy.api.IStrategy#onStop()
      */
    @Override
    public void onStop() throws JFException {
        flushBuffers();
    }

    /*
      * (non-Javadoc)
      *
      * @see com.dukascopy.api.IStrategy#onTick(com.dukascopy.api.Instrument,
      * com.dukascopy.api.ITick)
      */
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        // Write data to comma separated file
        if (instrument == Instrument.EURUSD && logData) {
            data.get(Period.TICK.name()).write(
                    String.format("%1$d,%2$1.6f,%3$1.6f,%4$12.6f,%5$12.6f\r\n",
                            tick.getTime(), tick.getAsk(), tick.getBid(),
                            tick.getAskVolume(), tick.getBidVolume()));
        }
    }

    /**
     *  flush all the buffers to files
     */
    public void flushBuffers() {
        if (logData) {
            for (PrintWriter w : data.values())
                w.close();
            System.out.print("flusing buffers");
        }
    }

    @Override
    protected void evaluateTradeEntry() throws JFException {
        throw new RuntimeException("Not implemented");
    }

    @Override
    protected void evaluateTradeExit() throws JFException {
        throw new RuntimeException("Not implemented");
    }

}
